<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Play;
use yii;


class PlayLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getQuan($query = [], $page = 1, $limit = 10)
    {
        $play = Play::find()->filterWhere($query)->andWhere(['type'=>0])->orderBy('play_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $play->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $play->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'play/quan',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getCard($query = [], $page = 1, $limit = 10)
    {
        $play = Play::find()->filterWhere($query)->andWhere(['type'=>1])->orderBy('play_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $play->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $play->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'play/card',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function editQuanById($id, $params, $uploadresult)
    {
        if(!$play = Play::findOne($id)) return false;
        $play->play_name = $params['play_name'];
        $play->price = $params['price'];
        $play->expire_time = $params['expire_time'];
        if($uploadresult)
            $play->play_pic = $uploadresult;
        return $play->save();
    }

    public function editCardById($id, $params, $uploadresult)
    {
        if(!$play = Play::findOne($id)) return false;
        $play->play_name = $params['play_name'];
        $play->price = $params['price'];
        $play->num = $params['num'];
        $play->expire_time = $params['expire_time'];
        $play->ext_price = $params['ext_price'];
        if($uploadresult)
            $play->play_pic = $uploadresult;
        return $play->save();
    }

    public function getById($id)
    {
        return Play::find()->where(['play_id' => $id])->asArray()->one();
    }


    public function saveStatus($id)
    {
        $play = Play::findOne($id);
        if($play->status)
            $play->status = 0;
        else
            $play->status = 1;
        return $play->save();
    }

    public function addQuan($params,$uploadresult){
        $play = new Play();
        $play->play_name = $params['play_name'];
        $play->price = $params['price'];
        $play->type = 0;
        $play->num = 1;
        $play->expire_time = $params['expire_time'];
        $play->play_pic = $uploadresult;
        return $play->save();
    }

    public function addCard($params,$uploadresult){
        $play = new Play();
        $play->play_name = $params['play_name'];
        $play->price = $params['price'];
        $play->type = 1;
        $play->num = $params['num'];
        $play->expire_time = $params['expire_time'];
        $play->ext_price = $params['ext_price'];
        $play->play_pic = $uploadresult;
        return $play->save();
    }

    public function getAllCard(){
        return Play::find()->where(['type'=>1])->asArray()->all();
    }

    public function getAllOkCard(){
        return Play::find()->where(['type'=>1,'status'=>1])->asArray()->all();
    }


}